package cn.atzxc.qy.service.impl;

import cn.atzxc.qy.domain.StudentsHelp;
import cn.atzxc.qy.domain.vo.StudentCategoryHelpVo;
import cn.atzxc.qy.mapper.StudentCategoryHelpVoMapper;
import cn.atzxc.qy.mapper.StudentHelpMapper;
import cn.atzxc.qy.service.StudentCategoryHelpVoService;
import cn.atzxc.qy.utils.JwtUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import io.jsonwebtoken.Claims;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class StudentCategoryHelpVoServiceImpl implements StudentCategoryHelpVoService {

    @Autowired
    private StudentCategoryHelpVoMapper studentCategoryHelpVoMapper;
    @Autowired
    private StudentHelpMapper studentHelpMapper;
    /**
     * 获取万能广场中的所有数据
     * @return
     */
    @Override
    public List<StudentCategoryHelpVo> getStudentCategoryHelpVoList() {
        List<StudentCategoryHelpVo> studentCategoryHelpVoList = studentCategoryHelpVoMapper.getStudentCategoryHelpVoList();
//        List<StudentCategoryHelpVo> studentCategoryHelpVoList = studentCategoryHelpVoMapper.getStudentCategoryHelpVoList();
        return studentCategoryHelpVoList;
    }

    /**
     * 根据ID，查询出指定条目的信详情
     * @param id
     * @return
     */
    @Override
    public StudentCategoryHelpVo selectOneStudentHelpVoById(Long id) {
        StudentCategoryHelpVo studentCategoryHelpVoById = studentCategoryHelpVoMapper.getStudentCategoryHelpVoById(id);
        return studentCategoryHelpVoById;
    }

    /**
     * 添加一个条数据
     * @param studenthelp
     * @return
     */
    @Override
    public int addStudentHelp(String token,Boolean isPublish,StudentCategoryHelpVo studenthelp) throws Exception {

        //1.解析出用户ID
        Claims claims = JwtUtil.parseJWT(token);
        String subject = claims.getSubject();
        Long userid = Long.parseLong(subject);//封装成为一个long类型
        System.out.println("解析出的ID为："+userid);

        //2.封装数据
//        StudentsHelp sHelp = new StudentsHelp();
//        if(studenthelp.getCategoryId() == null){
//            sHelp.setCategoryId(studenthelp.getCategoryId());//设置分类ID
//        }
//        if(studenthelp.getTitle() == null){
//            sHelp.setTitle(studenthelp.getTitle());//设置标题
//        }
//        if(studenthelp.getPrice() == null){
//            sHelp.setPrice(studenthelp.getPrice());//设置价格
//        }
//        if(studenthelp.getImageURL() == null){
//            sHelp.setImageURL(studenthelp.getImageURL());//设置URL
//        }
//        if(studenthelp.getRemark() == null){
//            sHelp.setRemark(studenthelp.getRemark());//获取备注
//        }
//        if(studenthelp.getDescription() == null){
//            sHelp.setDescription(studenthelp.getDescription());//设置描述
//        }

        studenthelp.setCreateTime(new Date());//创建时间
        studenthelp.setUserId(userid);//用户ID
        if(isPublish){
            studenthelp.setStatus_id(1L);//状态ID
        }else{
            studenthelp.setStatus_id(99L);
        }


        System.out.println("封装后的数据为："+studenthelp);

        //3.传递到mapper
        int num = studentCategoryHelpVoMapper.insertStudentHelp(studenthelp);

        return 0;
    }

    @Override
    public List<StudentCategoryHelpVo> getMyStudentCategoryHelpVoList(String token) throws Exception {
        //1.解析出用户ID
        Claims claims = JwtUtil.parseJWT(token);
        String subject = claims.getSubject();
        Long userid = Long.parseLong(subject);//封装成为一个long类型
        System.out.println("解析出的ID为："+userid);
        List<StudentCategoryHelpVo> studentCategoryHelpVoList = studentCategoryHelpVoMapper.getMyStudentCategoryHelpVoList(userid);

        return studentCategoryHelpVoList;
    }

    @Override
    public int deleteStudentHelpVo(String token, String shelp_id) throws Exception {
        //1.解析出用户ID
        Claims claims = JwtUtil.parseJWT(token);
        String subject = claims.getSubject();
        Long userid = Long.parseLong(subject);//封装成为一个long类型
        System.out.println("解析出的ID为："+userid);
        LambdaQueryWrapper<StudentsHelp> subjects = new LambdaQueryWrapper<>();
        subjects.eq(StudentsHelp::getShelpId,shelp_id);
        int delete = studentHelpMapper.delete(subjects);
        return delete;
    }

    @Override
    public Long getUserIdBySHelpId(Long id) {

        LambdaQueryWrapper<StudentsHelp> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(StudentsHelp::getShelpId,id);

        StudentsHelp studentsHelp = studentHelpMapper.selectOne(queryWrapper);
        if (studentsHelp == null) {
            return null;
        }
        return studentsHelp.getUserId();
    }
}
